home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 2 / LSD and 17bit Compendium Deluxe - Volume II.iso / a / prog / cprog / tdddconv.lha / writeoff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-25  |  5.2 KB  |  214 lines

  1. /* writeoff.c - dump the internal database to an OFF file
  2.  *            - written by Glenn M. Lewis - 10/29/91
  3.  */
  4.  
  5. static char rcs_id[] = "$Id: writeoff.c,v 1.4 1991/11/25 21:32:10 glewis Exp glewis $";
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include "ttdddlib.h"
  10.  
  11. static void process_DESC();
  12. static void process_points_DESC();
  13. static void process_faces_DESC();
  14. static void count_points_faces();
  15.  
  16. static int geom_count;
  17. static char filename[256];
  18. static char *rootname;
  19. static FILE *out, *outcol;
  20. static unsigned long poff;    /* Point index offset */
  21.  
  22. /* Here are a few necessary utilities */
  23.  
  24. static void send_XYZ(f)            /* Print a common string */
  25. XYZ_st *f;
  26. {
  27.     fprintf(out, "%g\t%g\t%g\n", f->val[0], f->val[1], f->val[2]);
  28. }
  29.  
  30. static void send_RGB(rgb)            /* Print a common string */
  31. RGB_st *rgb;
  32. {
  33.     fprintf(outcol, "%g\t%g\t%g\n", ((double)rgb->val[0])/255.0,
  34.         ((double)rgb->val[1])/255.0,
  35.         ((double)rgb->val[2])/255.0);
  36. }
  37.  
  38. /********************/
  39. /* The MAIN section */
  40. /********************/
  41.  
  42. int geom_flag;
  43.  
  44. int write_OFF(world, name, splitflag, geom_only)
  45. WORLD *world;
  46. char *name;
  47. int splitflag, geom_only;
  48. {
  49.     register OBJECT *o;
  50.     unsigned long total_points, total_faces;
  51.  
  52.     geom_flag = geom_only;
  53.     geom_count = 0;
  54.     rootname = name;
  55.     if (splitflag) {
  56.         for (o=world->object; o; o=o->next)
  57.             if (!o->extr) process_DESC(o);
  58.         return(1);
  59.     }
  60.  
  61.     strcpy(filename, rootname);
  62.     strcat(filename, ".geom");
  63.     if (!(out=fopen(filename, "w"))) {
  64.         fprintf(stderr, "Can't open '%s' for output.\n", filename);
  65.         return(0);
  66.     }
  67.     if (!geom_flag) {
  68.         strcpy(filename, rootname);
  69.         strcat(filename, ".ipcol");
  70.         if (!(outcol=fopen(filename, "w"))) {
  71.             fprintf(stderr, "Can't open '%s' for output.\n", filename);
  72.             fclose(out);
  73.             return(0);
  74.         }
  75.     }
  76.  
  77.     poff = total_points = total_faces = 0;
  78.  
  79.     for (o=world->object; o; o=o->next)
  80.         count_points_faces(o, &total_points, &total_faces);
  81.     fprintf(out, "%lu\t%lu\t%lu\n", total_points, total_faces, total_points*3L);
  82.     if (!geom_flag) fprintf(outcol, "%lu\n", total_faces);
  83.  
  84.     for (o=world->object; o; o=o->next)
  85.         process_points_DESC(o);
  86.     for (o=world->object; o; o=o->next)
  87.         process_faces_DESC(o);
  88.     
  89.     fclose(out);
  90.     if (!geom_flag) fclose(outcol);
  91.     return(1);
  92. }
  93.  
  94. static void count_points_faces(object, points, faces)
  95. register OBJECT *object;
  96. unsigned long *points, *faces;
  97. {
  98.     register OBJECT *obj;
  99.  
  100.     if (object->extr) return;
  101.     *points += object->desc->pcount;
  102.     *faces  += object->desc->fcount;
  103.  
  104.     for (obj=object->child; obj; obj=obj->next) {
  105.         if (!obj->extr) count_points_faces(obj, points, faces);
  106.     }
  107. }
  108.  
  109. static void process_points_DESC(object)
  110. OBJECT *object;
  111. {
  112.     register int i;
  113.     register OBJECT *obj;
  114.     register DESC *desc = object->desc;
  115.  
  116.     if (!desc->pcount || !desc->fcount) return;
  117.  
  118.     for (i=0; i<desc->pcount; i++) {
  119.         send_XYZ(&desc->pnts[i]);
  120.     }
  121.  
  122.     for (obj=object->child; obj; obj=obj->next) {
  123.         if (!obj->extr) process_points_DESC(obj);
  124.     }
  125. }
  126.  
  127. static void process_faces_DESC(object)
  128. OBJECT *object;
  129. {
  130.     register int i;
  131.     register OBJECT *obj;
  132.     register DESC *desc = object->desc;
  133.     register int p1, p2, p3;
  134.  
  135.     if (!desc->pcount || !desc->fcount) return;
  136.  
  137.     for (i=0; i<desc->fcount; i++) {
  138.     /* First check to make sure that this triangle is real */
  139.         p1 = desc->edge[(desc->face[i*3])<<1];
  140.         p2 = desc->edge[((desc->face[i*3])<<1)+1];
  141.         /* if (p1 == p2) continue;    /* How did *this* happen? */
  142.         p3 = desc->edge[(desc->face[i*3+2])<<1];
  143.         if (p1 == p3 || p2 == p3)
  144.             p3 = desc->edge[((desc->face[i*3+2])<<1)+1];
  145.         /* if (p1 == p3 || p2 == p3) continue; /* How did *this* happen? */
  146.  
  147.         fprintf(out, "3\t%lu\t%lu\t%lu\n", poff+p1+1, poff+p2+1, poff+p3+1);
  148.         if (!geom_flag) send_RGB(&desc->clst[i*3]);
  149.     }
  150.  
  151.     poff += desc->pcount;
  152.  
  153.     for (obj=object->child; obj; obj=obj->next) {
  154.         if (!obj->extr) process_faces_DESC(obj);
  155.     }
  156. }
  157.  
  158. static void process_DESC(object)
  159. OBJECT *object;
  160. {
  161.     register int i;
  162.     register OBJECT *obj;
  163.     register DESC *desc = object->desc;
  164.     register int p1, p2, p3;
  165.  
  166.     /* Process children first */
  167.     for (obj=object->child; obj; obj=obj->next) {
  168.         if (!obj->extr) process_DESC(obj);
  169.     }
  170.  
  171.     if (!desc->pcount || !desc->fcount) return;
  172.  
  173.     geom_count++;
  174.     sprintf(filename, "%s%03d.geom", rootname, geom_count);
  175.     if (!(out = fopen(filename, "w"))) {
  176.         fprintf(stderr, "Can't open '%s' for output.\n", filename);
  177.         return;
  178.     }
  179.     fprintf(out, "; %s\n", filename);
  180.     fprintf(out, "%u\t%u\t%u\n",
  181.         desc->pcount, desc->fcount, desc->pcount*3);
  182.     if (!geom_flag) {
  183.         sprintf(filename, "%s%03d.ipcol", rootname, geom_count);
  184.         if (!(outcol = fopen(filename, "w"))) {
  185.             fprintf(stderr, "Can't open '%s' for output.\n", filename);
  186.             return;
  187.         }
  188.         fprintf(outcol, "; %s\n", filename);
  189.         fprintf(outcol, "%u\n", desc->fcount);
  190.     }
  191.  
  192.     for (i=0; i<desc->pcount; i++) {
  193.         send_XYZ(&desc->pnts[i]);
  194.     }
  195.  
  196.     for (i=0; i<desc->fcount; i++) {
  197.     /* First check to make sure that this triangle is real */
  198.         p1 = desc->edge[(desc->face[i*3])<<1];
  199.         p2 = desc->edge[((desc->face[i*3])<<1)+1];
  200.         /* if (p1 == p2) continue;    /* How did *this* happen? */
  201.         p3 = desc->edge[(desc->face[i*3+2])<<1];
  202.         if (p1 == p3 || p2 == p3)
  203.             p3 = desc->edge[((desc->face[i*3+2])<<1)+1];
  204.         /* if (p1 == p3 || p2 == p3) continue; /* How did *this* happen? */
  205.  
  206.         fprintf(out, "3\t%u\t%u\t%u\n", p1+1, p2+1, p3+1);
  207.         if (!geom_flag) send_RGB(&desc->clst[i*3]);
  208.     }
  209.  
  210.     fclose(out);
  211.     if (!geom_flag) fclose(outcol);
  212. }
  213.  
  214.